package com.gjun.struts.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.gjun.struts.bean.BookJsonBean;
import com.gjun.struts.bean.CategoryJsonBean;
import com.gjun.struts.bean.GjunNewsJsonBean;
import com.gjun.struts.bean.ext.Book;
import com.gjun.struts.bean.ext.Category;
import com.gjun.struts.resource.dao.IBookDao;
import com.gjun.struts.resource.dao.ICategoryDao;
import com.gjun.struts.util.JSONHelper;

public class MgrBooksAction {
	
	
	private Date starttimer;
	
	private Date endtimer;
	
	private int pageSize = 10; 
    
    private int totalSize;
    
    private int totalPage;
    
    private int curPage;
    
    private int id;
    
    private int[] delId;
    
    private int type;
    private int firstCategory;
    private String author;
    private String productName;
    private String publishing;
    private Date opendate;
    private Date enddate;
    private String infoContent;
    
	private String result;


	private ICategoryDao categoryDao;
	
	private IBookDao bookDao;

	public int getType() {
		return type;
	}
	public String getResult() {
		return result;
	}
	
	public void setResult(String result) {
		this.result = result;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getPublishing() {
		return publishing;
	}

	public void setPublishing(String publishing) {
		this.publishing = publishing;
	}

	public void setType(int type) {
		this.type = type;
	}

	public Date getStarttimer() {
		return starttimer;
	}

	public void setStarttimer(Date starttimer) {
		this.starttimer = starttimer;
	}

	public Date getEndtimer() {
		return endtimer;
	}

	public void setEndtimer(Date endtimer) {
		this.endtimer = endtimer;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int[] getDelId() {
		return delId;
	}

	public void setDelId(int[] delId) {
		this.delId = delId;
	}

	public int getFirstCategory() {
		return firstCategory;
	}

	public void setFirstCategory(int firstCategory) {
		this.firstCategory = firstCategory;
	}

	public Date getOpendate() {
		return opendate;
	}

	public void setOpendate(Date opendate) {
		this.opendate = opendate;
	}

	public Date getEnddate() {
		return enddate;
	}

	public void setEnddate(Date enddate) {
		this.enddate = enddate;
	}

	public String getInfoContent() {
		return infoContent;
	}

	public void setInfoContent(String infoContent) {
		this.infoContent = infoContent;
	}


	public ICategoryDao getCategoryDao() {
		return categoryDao;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public IBookDao getBookDao() {
		return bookDao;
	}

	public void setBookDao(IBookDao bookDao) {
		this.bookDao = bookDao;
	}

	public String initFirstCategory() throws Throwable {
		List<Category> categories = categoryDao.findAllCategory();
		List<CategoryJsonBean> list = new ArrayList<CategoryJsonBean>();
		for (Category category : categories) {
			CategoryJsonBean tempt = new CategoryJsonBean(category.getId(),
					category.getName());
			list.add(tempt);
		}
		result = JSONHelper.categoryToJson(list);
		return "success";

	}
	
	public String loadNewsData(){
		String conditions = "";
		if(type == -1){
			if(starttimer != null){
				conditions += "b.publishTime";
				if(endtimer != null){
					conditions += "b.publishTime";
				}
			}
		} else{
			if(starttimer != null){
				conditions += "b.publishTime";
				if(endtimer != null){
					conditions += "b.publishTime";
				}
			}
		}
		
		if(author != null && !author.isEmpty()){
			conditions += " and b.author='"+author +"'";
		}
		
		if(productName != null && !productName.isEmpty()){
			conditions += " and b.productName='"+productName +"'";
		}
		
		if(publishing != null && !publishing.isEmpty()){
			conditions += " and b.publishing='"+publishing +"'";
		}
		List<Book> books = bookDao.findBookByConditions(conditions);
		totalSize = books.size();
		totalPage = (totalSize + pageSize - 1) /pageSize;
		if(curPage == 0)
			curPage = 1;
		int start = (curPage - 1)*pageSize;
		List<Book> books2 = bookDao.findBookByConditions(conditions,start,pageSize);
		List<BookJsonBean> booksJson = new ArrayList();
		for (Book book : books2) {
			BookJsonBean bean = new BookJsonBean(book);
			booksJson.add(bean);
		}
		result = JSONHelper.bookToJson(booksJson);
		return "success";
	}
	
	public String delBooksById(){
		//bookDao.delBooksById(id);
		Book book = bookDao.getBookById(id);
		book.setHasDeleted(1);
		bookDao.delBooks(book);
		result = "delBooksSuccess";
		return "success";
	}
	
	public String batchDelBooks(){
		for (int id : delId) {
			Book book = bookDao.getBookById(id);
			book.setHasDeleted(1);
			bookDao.delBooks(book);
			book = bookDao.getBookById(id);
			book.setAddTime(1);
			bookDao.delBooks(book);
		}
		result = "delBatchSuccess";
		return "success";
	}
	
	public String loadBooksEditById(){
		Book book = bookDao.getBookById(id);
		BookJsonBean bean = new BookJsonBean(book);
    	ServletActionContext.getRequest().getSession().setAttribute("editBookBean",bean);
		result = "edit_to_next_page";
		return "success";
	}
	
	
}
